package com.dididoctor.patient.Dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.dididoctor.patient.Bean.DiaeasesLitapal;
import com.dididoctor.patient.MV.adapter.CommonAdapter;
import com.dididoctor.patient.MV.adapter.ViewHolder;
import com.dididoctor.patient.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @version 2.0
 * @类描述：
 * @项目名称：ddgh
 * @类名称： DiseaseListDialog
 * @包名称： com.dididoctor.patient.Dialog
 * @创建人： zhanjunjie
 * @创建时间：2016/4/12 10:08
 * @修改人：
 * @修改时间：2016/4/12 10:08
 * @修改备注： new parent class(用一句话描述修改了什么)
 */
public class DiseaseListDialog extends Dialog {

    private List<DiaeasesLitapal> diaeasesLitapals = new ArrayList<DiaeasesLitapal>();
    private ListView listView;
    private Context context;
    private ImageView mImgCancel;
    private DiseaseListAdapter adapter;

    private onItemClickListener mListener;

    public DiseaseListDialog(Context context, List<DiaeasesLitapal> diaeasesLitapals, onItemClickListener listener) {
        super(context, R.style.Dialog);
        this.context = context;
        this.mListener = listener;
        this.diaeasesLitapals = diaeasesLitapals;
        setContentView(R.layout.dialog_select_list);
        initview();
    }

    private void initview() {
        mImgCancel = (ImageView) findViewById(R.id.img_cancel);
        mImgCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                mListener.setfinis();
            }
        });
        listView = (ListView) findViewById(R.id.listview);
        adapter = new DiseaseListAdapter(context, diaeasesLitapals);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mListener != null) {
                    mListener.itemClick(position);
                }
                dismiss();
            }
        });
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            mListener.setfinis();
        }
        return super.onKeyDown(keyCode, event);
    }

    class DiseaseListAdapter extends CommonAdapter<DiaeasesLitapal> {

        private TextView mTvDiaease;

        public DiseaseListAdapter(Context context, List<DiaeasesLitapal> datas) {
            super(context, datas, R.layout.item_diaease);
        }

        @Override
        public void convert(int position, ViewHolder helper, DiaeasesLitapal item) {
            mTvDiaease = helper.getView(R.id.tv_diasease);
            mTvDiaease.setText(item.getName());
        }
    }

    public interface onItemClickListener {
        void itemClick(int position);

        void setfinis();
    }

}
